package com.gongj.apppay.controller;

import com.gongj.commons.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Set;

@RestController
@RequestMapping("/api/rest")
public class RestTemplateController {

    @Autowired
    @Qualifier("restTemplateBean")
    RestTemplate restTemplate;

    //测试GET====================
    @GetMapping
    public void getName(String name) throws UnsupportedEncodingException {
        String rest = restTemplate.getForObject("http://appUser/api/user/getName?name={1}", String.class,name);
        System.out.println("getForObject:" + rest);

        ResponseEntity<String> entity = restTemplate.getForEntity("http://appUser/api/user/getName?name={1}", String.class, "哈哈哈哈");

        HashMap<String , String > map = new HashMap<>();
        map.put("name","嘿哈嘿哈");
        entity = restTemplate.getForEntity("http://appUser/api/user/getName?name={name}", String.class,map);

        String url = "http://appUser/api/user/getName?name=" + URLEncoder.encode("红红火火恍恍惚惚","UTF-8");
        URI uri = URI.create(url);
        entity =  restTemplate.getForEntity(uri,String.class);


        String body = entity.getBody();
        System.out.println("body:"+body);
        HttpStatus statusCode = entity.getStatusCode();
        System.out.println("statusCode:" + statusCode);
        int statusCodeValue = entity.getStatusCodeValue();
        System.out.println("statusCodeValue" + statusCodeValue);
        HttpHeaders headers = entity.getHeaders();
        Set<String> keySet = headers.keySet();
        System.out.println("===============headers===========");
        for (String s : keySet) {
            System.out.println("keySet:" + headers.get(s));
        }
        System.out.println("headers" +    headers);
    }

    @GetMapping("getName2")
    public void getName2(String name) throws UnsupportedEncodingException {
        String rest = restTemplate.getForObject("http://appUser/api/user/getName?name={1}", String.class,name);
        System.out.println("getForObject1:" + rest);
        HashMap<String, Object> map = new HashMap<String,Object>();
        map.put("name",name);
        rest = restTemplate.getForObject("http://appUser/api/user/getName?name={name}", String.class,map);
        System.out.println("getForObject2:" + rest);
        //如果是中文记得转码
        String url = "http://appUser/api/user/getName?name=" + URLEncoder.encode(name,"UTF-8");
        URI uri = URI.create(url);
        rest = restTemplate.getForObject(uri,String.class);
        System.out.println("getForObject3:" + rest);
    }

    //测试POST====================

    @GetMapping("/addUser")
    public void addUser(){
        MultiValueMap map = new LinkedMultiValueMap();
        map.add("id","1");
        map.add("name","gongj");
        ResponseEntity<User> userResponseEntity = restTemplate.postForEntity("http://appUser/api/user/addUser", map, User.class);
        System.out.println(userResponseEntity);
        User user = new User();
        user.setId("87777");
        user.setName("哈哈哈哈哈");
        userResponseEntity = restTemplate.postForEntity("http://appUser/api/user/addUserJson", user, User.class);
        System.out.println(userResponseEntity);

        user = restTemplate.postForObject("http://appUser/api/user/addUser", map, User.class);
        System.out.println("key-value：" + user);

        user.setId("99");
        user.setName("高兴哈哈哈");
        user = restTemplate.postForObject("http://appUser/api/user/addUserJson", user, User.class);
        System.out.println("json：" + user);
    }


    //测试PUT====================
    @RequestMapping("/put")
    public void put() {
        User user = new User();
        user.setId("99");
        user.setName("高兴哈哈哈");
        restTemplate.put("http://appUser/api/user/put/{1}", user,55);
    }

    //测试delete====================
    @RequestMapping("/del")
    public void del() {
        restTemplate.delete("http://appUser/api/user/del/{1}", 55);
    }
    //测试delete====================
    @RequestMapping("/exchange")
    public void exchange() {
        User user = new User();
        user.setId("99");
        user.setName("高兴哈哈哈");
        HttpHeaders headers = new HttpHeaders();//header参数
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<User> request = new HttpEntity<>(user,headers); //组装
        ResponseEntity<User> exchange = restTemplate.exchange("http://appUser/api/user/addUserJson", HttpMethod.POST, request, User.class);
        System.out.println("json：" + exchange);
    }

    @GetMapping("/register")
    public void register(){
        MultiValueMap map = new LinkedMultiValueMap();
        map.add("id","1");
        map.add("name","gongj");
        URI uri = restTemplate.postForLocation("http://appUser/register", map);
        System.out.println(uri);
        String s = restTemplate.getForObject(uri, String.class);
        System.out.println(s);
    }
}
